package com.swb.mybatis04emp.web.controller;

import com.github.pagehelper.PageInfo;
import com.swb.mybatis04emp.common.StaticConst;
import com.swb.mybatis04emp.entity.Emp;
import com.swb.mybatis04emp.service.EmpService;
import com.swb.mybatis04emp.service.impl.EmpServiceImpl;
import com.swb.mybatis04emp.utils.MyBatisUtils;
import com.swb.mybatis04emp.utils.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.sql.Statement;
import java.util.List;

/**
 * @title: EmpController
 * @author: 流沐颖
 * @date:2022/6/2 8:58
 * @description:
 * @version: study
 */
@WebServlet("/emp")
public class EmpController extends BaseServlet {
    /**
     * 业务层对象
     */
    private EmpService empService = new EmpServiceImpl();


    /**
     * 执行登录
     *
     * @param request
     * @param response
     * @return
     */
    public String doLogin(HttpServletRequest request, HttpServletResponse response) {
        /**
         * 01 接前端数据
         */
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username);

        /**
         * 02 调用业务层
         */
        boolean flag = empService.doLogin(username, password);

        /**
         * 03 做出响应
         */
        if (flag) {
            // 将登录信息存入session
            request.getSession().setAttribute(StaticConst.IS_LOGIN, username);
            // 重定向到查找方法
            return StaticConst.REDIRECT + "loginSuccess.jsp";

        } else {
            System.out.println("用户名或密码错误！");
            request.setAttribute("msg", "用户名或密码错误！");
            return StaticConst.FORWARD + "login.jsp";
        }

    }


    /**
     * 查询所有员工
     *
     * @param request
     * @param response
     * @return
     */
    public String getAll(HttpServletRequest request, HttpServletResponse response) {

        /**
         * 01 接前端数据
         */
        System.out.println("调用查找全部emp的方法");
        /**
         * 02 调用业务层
         */
        List<Emp> empList = empService.getAll();
        /**
         * 03 做出响应
         */
        if (empList != null) {
            // 将结果保存到请求中并转发到下一个页面
            request.setAttribute("empList", empList);

            return StaticConst.FORWARD + "index.jsp";

        }
        return StaticConst.REDIRECT + "404.jsp";


    }

    /**
     * 添加员工信息
     *
     * @return
     */
    public String addEmp(HttpServletRequest request, HttpServletResponse response) {
        /**
         * 01 接前端数据
         */
        String ename = request.getParameter("ename");
        String esex = request.getParameter("esex");
        double salary = Double.valueOf(request.getParameter("salary"));
        String pwd = request.getParameter("pwd");
        Date bornDate = Date.valueOf(request.getParameter("bornDate"));
        int dno = Integer.valueOf(request.getParameter("dno"));
        // 初始化实体
        Emp emp = new Emp();
        emp.setEname(ename);
        emp.setEsex(esex);
        emp.setSalary(salary);
        emp.setPwd(pwd);
        emp.setBornDate(bornDate);
        emp.setDno(dno);
        System.out.println(emp);
        /**
         * 02 调用业务层
         */
        int re = empService.addEmp(emp);
        /**
         * 03 做出响应
         */
        if (re > 0) {
            // 增加记得提交
            MyBatisUtils.commit();
            return StaticConst.REDIRECT + "loginSuccess.jsp";

        } else {
            // 失败后回滚数据
            MyBatisUtils.rollback();
            return StaticConst.REDIRECT + "404.jsp";
        }
    }


    /**
     * 删除员工信息
     */
    public String delEmp(HttpServletRequest request, HttpServletResponse response) {
        /**
         * 01 接前端数据
         */
        int eno = StrUtils.StrToInt(request.getParameter("eno"));
        /**
         * 02 调用业务层
         */
        int i = empService.delEmp(eno);
        MyBatisUtils.commit();
        /**
         * 03 做出响应
         */
        if (i > 0) {

            return StaticConst.REDIRECT + "loginSuccess.jsp";
        } else {
            MyBatisUtils.rollback();
            return StaticConst.REDIRECT + "404.jsp";
        }

    }


    /**
     * 根据主键查出要修改的信息
     */
    public String getEmpByEno(HttpServletRequest request, HttpServletResponse response) {
        /**
         * 01 接前端数据
         */
        int eno = StrUtils.StrToInt(request.getParameter("eno"));
        System.out.println(eno);
        /**
         * 02 调用业务层
         */
        Emp emp = empService.getEmpByEno(eno);
        System.out.println(emp);
        /**
         * 03 做出响应
         */
        if (emp != null) {
            // 将数据保存到请求中  并转发到更新页面
            request.setAttribute("emp", emp);
            return StaticConst.FORWARD + "updateEmp.jsp";

        } else {
            return StaticConst.REDIRECT + "404.jsp";
        }
    }


    /**
     * 修改员工信息
     */
    public String updateEmp(HttpServletRequest request, HttpServletResponse response) {
        /**
         * 01 接前端数据
         */
        int eno=StrUtils.StrToInt(request.getParameter("eno"));
        String ename = request.getParameter("ename");
        String esex = request.getParameter("esex");
        double salary = Double.valueOf(request.getParameter("salary"));
        String pwd = request.getParameter("pwd");
        Date bornDate = Date.valueOf(request.getParameter("bornDate"));
        int dno = Integer.valueOf(request.getParameter("dno"));
        // 初始化实体
        Emp emp = new Emp();
        emp.setEno(eno);
        emp.setEname(ename);
        emp.setEsex(esex);
        emp.setSalary(salary);
        emp.setPwd(pwd);
        emp.setBornDate(bornDate);
        emp.setDno(dno);
        System.out.println(emp);

        /**
         * 02 调用业务层
         */
        int re = empService.update(emp);
        MyBatisUtils.commit();
        /**
         * 03 做出响应
         */
        if (re > 0) {
            System.out.println("修改成功！");

            return StaticConst.REDIRECT+"updateSuccess.jsp";
        } else {
            MyBatisUtils.rollback();
            return StaticConst.REDIRECT+"404.jsp";
        }


    }


    /**
     * 分页
     */
    public String getEmpByPages(HttpServletRequest request, HttpServletResponse response){
        //1 页码
        String pageIndex = request.getParameter("pageIndex");
        // 从前台传来的是个空值  则默认将第一页作为默认页
        Integer pageNum = pageIndex == null ? 1 : Integer.valueOf(pageIndex);
        //访问业务层获取分页数据
        PageInfo<Emp> pi = empService.getEmpByPages(pageNum, 5);
        request.setAttribute("pi", pi);
        return StaticConst.FORWARD+"index.jsp";
    }
}
